﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Constants;
using Unosquare.Labs.EmbedIO.Modules;

namespace ChangeWifiWebApi
{
  internal class Program
  {
    static void Main(string[] args)
    {
      var url1 = "http://+:9000/";
      var url2 = "http://localhost:9000/";
      var url3 = "http://192.168.191.1:9000/";
      if (args.Length > 0)
      {
        url1 = args[0];
      }
      // Our web server is disposable.
      //using (var server = new WebServer(new[] { url1, url3 }, RoutingStrategy.Regex))
      using (var server = new WebServer(new[] { url1, url3 }, RoutingStrategy.Regex))
      {
        server.RegisterModule(new LocalSessionModule());

        server.RegisterModule(new StaticFilesModule(@"html"));
        server.Module<StaticFilesModule>().UseRamCache = true;
        server.Module<StaticFilesModule>().DefaultExtension = ".html";
        //server.Module<StaticFilesModule>().DefaultDocument = "index.html";

        server.RegisterModule(new WebApiModule());
        server.Module<WebApiModule>().RegisterController<WifiController>();

        //IVirtualRouter vr = new VirtualRouterAccessor();
        Task.Run(() =>
        {
          try
          {
            //vr.Start("tbox-test", "12345678");

          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.ToString());
          }
        });
        var task = server.RunAsync();

        // display
        Console.WriteLine($"Server listening on {url1}");
        Console.WriteLine($"Server listening on {url3}");

#if DEBUG
        var browser = new Process()
        {
          StartInfo = new ProcessStartInfo(url2) { UseShellExecute = true }
        };
        browser.Start();
#endif

        while (true)
        {
          Console.ReadKey(true);
        }
      }
    }
  }
}